﻿Public Class PackageFileActivityControl

    Public Sub Start()
        SetupEvents(True)
    End Sub
    Public Sub [Stop]()
        SetupEvents(False)
    End Sub

    Private Sub SetupEvents(ByVal startListening As Boolean)
        If startListening Then
            AddHandler Engine.Manager.PackagingDaemonActivityGUI, AddressOf EventHandler_Activity
        Else
            RemoveHandler Engine.Manager.PackagingDaemonActivityGUI, AddressOf EventHandler_Activity
        End If
    End Sub

    Private mLastFilePath As String = Nothing
    Private mLastBytesProcessed As Long = 0
    Private mLastAction As String = Nothing
    Private mLastTimeStamp As Long = 0

    Private Sub EventHandler_Activity(ByVal e As Engine.Manager.PackagingDaemonActivityEventArgs)
        Try
            Dim strTmp As String = String.Empty
            If Not String.IsNullOrEmpty(e.FilePath) Then strTmp = IO.Path.GetFileName(e.FilePath)
            If txtCurrentFile.Text <> strTmp Then
                txtCurrentFile.Text = strTmp
                txtCurrentFile.Select(strTmp.Length, 0)
                txtCurrentFile.ScrollToCaret()
            End If

            strTmp = String.Empty
            If e.Job IsNot Nothing Then strTmp = e.Job.Name
            If txtCurrentJob.Text <> strTmp Then txtCurrentJob.Text = strTmp

            strTmp = String.Empty
            If e.FileSize > 0 Then strTmp = Framework.Misc.ToReadableFileSize(e.FileSize)
            If txtFileSize.Text <> strTmp Then txtFileSize.Text = strTmp

            strTmp = e.FilesInQueue.ToString
            If txtQueueLength.Text <> strTmp Then txtQueueLength.Text = strTmp

            If pgbCurrentFile.Value <> e.ActionCompletedPercent Then pgbCurrentFile.Value = e.ActionCompletedPercent

            If lblCurrentAction.Text <> e.Action Then lblCurrentAction.Text = e.Action

            If mLastFilePath <> e.FilePath Then
                mLastFilePath = e.FilePath
                mLastBytesProcessed = e.ActionCompletedPercent
                mLastTimeStamp = Framework.Performance.Counter.GetPerfCounter
                txtSpeed.Text = String.Empty
            ElseIf mLastAction <> e.Action Then
                mLastAction = e.Action
                mLastBytesProcessed = e.ActionCompletedPercent
                mLastTimeStamp = Framework.Performance.Counter.GetPerfCounter
                txtSpeed.Text = String.Empty
            Else
                Dim lNewTimeStamp As Long = Framework.Performance.Counter.GetPerfCounter
                Dim iTime As Integer = Framework.Performance.Counter.GetTime(mLastTimeStamp, lNewTimeStamp)
                mLastTimeStamp = lNewTimeStamp
                Dim lBytes As Long = e.ActionBytesProcessed - mLastBytesProcessed
                mLastBytesProcessed = e.ActionBytesProcessed

                If iTime = 0 Or lBytes = 0 Then
                    txtSpeed.Text = String.Empty
                Else
                    Dim iBytesPerSec As Integer = CInt(lBytes * 1000 / iTime)
                    txtSpeed.Text = Framework.Misc.ToReadableSpeed(iBytesPerSec)
                End If
            End If
        Catch ex As Exception
            Framework.Misc.HandleException(Me.FindForm, ex)
        End Try
    End Sub

End Class
